package com.zr.beijian.service.impl;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.zr.beijian.JPARepository.BeiJianJapRepository;
import com.zr.beijian.bean.BeiJian;
import com.zr.beijian.bean.QBeiJian;
import com.zr.beijian.model.BeiJianSelectVo;
import com.zr.beijian.model.BeiJianUpdateVo;
import com.zr.beijian.model.BeiJianVo;
import com.zr.beijian.redis.RedisAnnotation;
import com.zr.beijian.redis.RedisRepository;
import com.zr.beijian.service.BeiJianService;
import com.zr.beijian.utils.AllRecords;
import com.zr.beijian.utils.EnumName;
import com.zr.beijian.utils.Result;
import com.zr.beijian.utils.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class BeiJianServiceImpl implements BeiJianService {
    @Autowired
    private RedisAnnotation redisAnnotation;
    @Autowired
    private BeiJianJapRepository beiJianRepository;
    @Autowired
    private RedisRepository redisRepository;
    @Override
    @Transactional
    public Result add(BeiJianVo beiJianVo) {
        BeiJian beiJian = new BeiJian();
        Date date = new  Date();
        beiJian.setCreateid(1);
        beiJian.setCreatename("yuanbo");
        beiJian.setCreatetime(date);
        beiJian.setUpdateid(1);
        beiJian.setUpdatename("yuanbo");
        beiJian.setUpdatetime(date);
        BeanUtils.copyProperties(beiJianVo,beiJian);
        if(beiJianRepository.findBeiJianByCode(beiJian.getCode())!=null){
               return ResultVo.error(500,"编码已存在");
        }
        if(beiJianRepository.findBeiJianByName(beiJian.getName())!=null){
               return ResultVo.error(500,"名称已存在");
        }
        beiJianRepository.save(beiJian);
        return ResultVo.success(beiJianVo);
    }

    @Override
    public BeiJian findById(int id) {
        return redisAnnotation.findOne(id);
    }

    @Override
    @Transactional
    public Result update(BeiJianUpdateVo beiJianUpdateVo) {
        BeiJian beiJian = redisRepository.findOne(beiJianUpdateVo.getId());
        Date date = new  Date();
        beiJian.setUpdateid(1);
        beiJian.setUpdatename("yuanbo");
        beiJian.setUpdatetime(date);
        BeanUtils.copyProperties(beiJianUpdateVo,beiJian);
        redisAnnotation.save(beiJian);
        return ResultVo.success(beiJianUpdateVo);
    }

    @Override
    public Result<AllRecords> queryByCodeAndName(BeiJianSelectVo beiJianSelectVo) {
        BooleanExpression booleanExpression = Expressions.asBoolean(true).isTrue();
        if(!StringUtils.isEmpty(beiJianSelectVo.getCode())){
            booleanExpression =booleanExpression.and(QBeiJian.beiJian.code.contains(beiJianSelectVo.getCode()));
        }
        if(!StringUtils.isEmpty(beiJianSelectVo.getName())){
            booleanExpression =  booleanExpression.and(QBeiJian.beiJian.name.contains(beiJianSelectVo.getName()));
        }
        if(!StringUtils.isEmpty(beiJianSelectVo.getState())){
            booleanExpression = booleanExpression.and(QBeiJian.beiJian.state.eq(beiJianSelectVo.getState()));
        }
        Page<BeiJian> page = beiJianRepository.findAll(booleanExpression,new PageRequest(beiJianSelectVo.getOffset(), beiJianSelectVo.getPageSize(), new Sort(Sort.Direction.DESC, "id")));
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(beiJianSelectVo.getPageIndex());
        allRecords.setPageSize(beiJianSelectVo.getPageSize());
        allRecords.setTotalNumber(page.getNumber());
        allRecords.setDataList(page.getContent());
        allRecords.resetTotalNumber(page.getNumber());
        return ResultVo.success(allRecords);
    }

    @Override
    @Transactional
    public void delete(int id) {
        redisAnnotation.delete(id);
    }
}
